package com.lam.dormitory.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lam.dormitory.entity.Admin;
import com.lam.dormitory.service.IAdminService;
import com.lam.dormitory.service.IDormitoryService;
import com.lam.dormitory.service.IRoleService;
import com.lam.dormitory.util.ResponseTemplate;
import com.lam.dormitory.vo.AdminVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author lam
 * @since 2019-12-26
 */
@Api(tags = "管理员接口")
@RestController
@RequestMapping("/admin")
public class AdminController {
    @Autowired
    private IAdminService adminService;
    @Autowired
    private IRoleService roleService;
    @Autowired
    private IDormitoryService dormitoryService;

    /**
     * 管理员详情
     * @param userId
     * @return
     */
    @ApiOperation(value = "管理员详情", notes = "根据用户userId管理员详情")
    @RequiresRoles(value = {"admin", "superadmin"}, logical = Logical.OR)
//    @RequiresPermissions(value = "admin:detail")
    @PostMapping("/detail")
    public ResponseTemplate adminDetailByUserId(Long userId) {
        Admin admin = new Admin();
        admin.setUserId(userId);
        List<AdminVo> adminList = adminService.selectByAdmin(admin);
        return new ResponseTemplate(200, "管理员详情返回成功",adminList);
    }

    /*编辑管理员*/
    @ApiOperation(value = "编辑管理员", notes = "编辑管理员")
    @PostMapping("/edit")
//    @RequiresPermissions("role:edit")
    @RequiresRoles(value = {"admin", "superadmin"}, logical = Logical.OR)
    public ResponseTemplate editAdmin(Admin admin) {
        QueryWrapper<Admin> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(Admin::getUserId, admin.getUserId());
        boolean b = adminService.update(admin, wrapper);
        if (b) {
            return new ResponseTemplate(200, "编辑成功");
        } else {
            return new ResponseTemplate(500, "编辑失败");
        }
    }

    /**
     * 管理员列表数据
     * @param pageNum
     * @param pageSize
     * @return
     */
    @ApiOperation(value = "管理员列表数据", notes = "根据adminNo,adminName,adminAge,adminSex,adminPosition,adminPhone查询管理员列表数据,pageNum 默认为 1，pageSize默认为 0")
    @PostMapping("/list")
//    @RequiresPermissions("role:list")
    @RequiresRoles(value = {"admin", "superadmin"}, logical = Logical.OR)
    public ResponseTemplate pageAdmins(Admin admin,@RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "0") Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<AdminVo> adminList = adminService.selectByAdmin(admin);
        PageInfo<AdminVo> pageInfo = new PageInfo<>(adminList);
        return new ResponseTemplate(200, "获取管理员列表数据成功", adminList, pageInfo.getTotal());
    }

    @ApiOperation(value = "删除管理员", notes = "根据userId删除管理员")
    @RequiresRoles(value = {"superadmin", "admin"}, logical = Logical.OR)
//    @RequiresPermissions("user:delete")
    @PostMapping("/delete")
    public ResponseTemplate deleteAdmin(Long userId) {
        QueryWrapper<Admin> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(Admin::getUserId,userId);
        boolean result = adminService.remove(wrapper);
        if (result) {
            return new ResponseTemplate(200, "管理员删除成功");
        }
        return new ResponseTemplate(500, "管理员删除失败");
    }

    @ApiOperation(value = "管理员展示列表", notes = "查询所有管理员和已被分配宿舍的管理员")
    @PostMapping("/showList")
//    @RequiresPermissions("role:list")
    @RequiresRoles(value = {"admin", "superadmin"}, logical = Logical.OR)
    public ResponseTemplate selectAdmin(){
        Map<String,Object> map  = new HashMap<>();
        List<Admin> allAdmin = adminService.list();
        QueryWrapper<Admin> wrapper = new QueryWrapper<>();
        wrapper.isNotNull("dor_id").notIn("dor_id",0);
        List<Admin> showAdmin = adminService.list(wrapper);
        map.put("allAdmin",allAdmin);
        map.put("showAdmin",showAdmin);
        return new ResponseTemplate(200,"查询成功",map);
    }

    @ApiOperation(value = "查询职位", notes = "查询管理员职位无重复")
    @PostMapping("/positionList")
//    @RequiresPermissions("role:list")
    @RequiresRoles(value = {"admin", "superadmin"}, logical = Logical.OR)
    public ResponseTemplate positionList(){
        QueryWrapper<Admin> queryWrapper = new QueryWrapper<>();
        queryWrapper.groupBy("admin_position");
        List<Admin> list = adminService.list(queryWrapper);
        return new ResponseTemplate(200,"查询成功",list);
    }

}
